<?php

print '<div class="module">
<h3 onclick="CMS.toggleSection(this);">Recent Sessions</h3>
<div class="content">
<table class="data_grid">
	<thead>
		<tr>
		<th>User</th>
		<th>Start</th>
		<th>End</th>
		<th>Duration</th>
		<th>Content Edits</th>
		</tr>
	</thead>
	<tbody class="records">';

if ($q = $this->db->query("SELECT * FROM ".BLACKBIRD_TABLE_PREFIX."sessions ORDER BY start_time DESC LIMIT 5")) {
	$rc = 0;

	foreach($q as $row){
		$classA = Array();
		($rc%2==0) ? '' : $classA[] = 'odd';
		$click = 'onclick="window.location = \'' . BLACKBIRD_ROOT . 'browse/'.BLACKBIRD_TABLE_PREFIX.'history/?sort_col=id%20DESC&amp;limit=10000&amp;filter_session_id=' . $row['session_id'] . '\';"';
		if($q_edits = $this->db->query("SELECT * FROM ".BLACKBIRD_TABLE_PREFIX."history WHERE session_id = '$row[session_id]'")){
			$e = count($q_edits);
		}else{
			$e = '';
			$click = '';
			$classA[] = 'locked';
		
		}
	
		$class = 'class="' . join(" ",$classA) . '"';
	
		print "<tr $class>";
	
	
	
		$deets = $this->db->queryRow("SELECT firstname,lastname,email FROM " . BLACKBIRD_USERS_TABLE . " WHERE id = '$row[user_id]'");
		print "<td $click>" . $deets['lastname'] . ', ' . $deets['firstname'] . '</td>';
		print "<td $click>" . $row['start_time'] . '</td>';
	
		if($row['end_time'] == '0000-00-00 00:00:00'){
			$et = '~';
			$diff = Utils::getTimeDifference($row['start_time'],Utils::now());
		}else{
			$et = $row['end_time'];
			$diff = Utils::getTimeDifference($row['start_time'],$row['end_time']);
		}
	
		print "<td $click>" . $et . '</td>';
		print "<td $click>" . $diff['hours'] . ':' . $diff['minutes'] . '</td>';
	
	
	
		print "<td $click>" . $e . '</td>';			
		print '</tr>';
		$rc++;

	}
}
		

print '
	</tbody>
</table>
</div>
</div>';